package DataAccessObject.entity; 

import java.text.ParsePosition;
import java.text.SimpleDateFormat;

import entity.Alumno;
import DataAccessObject.DAO;
import DataAccessObject.entity.AlumnoDAO;

public class AlumnoDAO extends DAO<Alumno.Pk, Alumno> {
	
	public AlumnoDAO() throws Exception {
		super();
	}// Constructor
	
  public Alumno insert(int dni, String nombre, String fecha) {
    Alumno entity = findById(dni);
    if (entity != null) {
      System.out.println("Valor insertado");
      return null;
    }//if
    entity = new Alumno();
    entity.setDni(dni);
    entity.setNombre(nombre);
    entity.setFechaN(new SimpleDateFormat("yyyy-MM-dd").parse(fecha, new ParsePosition(0)));
    try {
      this.persist(entity);
    } catch (Exception e) {
      e.printStackTrace();
      entity = null;
    }// try-catch
    return entity;
  }// insert
  
  public Alumno insert(Alumno alu) {
    Alumno entity = findById(alu.getDni());
    if (entity != null) {
      System.out.println("Valor insertado");
      return null;
    }//if
    try {
      this.persist(alu);
    } catch (Exception e) {
      e.printStackTrace();
      alu = null;
    }// try-catch
    return alu;
  }// insert

  public Alumno update(Alumno alu) {
    try {
      this.merge(alu);
    } catch (Exception e) {
      e.printStackTrace();
      alu = null;
    }// try-catch
    return alu;
  }// update

  public Alumno delete(int dni) {
    Alumno entity = null;
    Alumno.Pk id = new Alumno.Pk();
    id.setDni(dni);
    try { entity = this.remove(id); }
    catch (Exception e) { e.printStackTrace(); }
    return entity;
  }// delete
  
  public Alumno findById(int dni) {
    Alumno.Pk id = new Alumno.Pk();
    id.setDni(dni);
    return findById(id);
  }// findById

}// class AlumnoDAO